package com.hctforgreen.greenservice.ui.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.TerminalPaperActivity;
import com.hctforgreen.greenservice.model.BaseEntity;
import com.hctforgreen.greenservice.model.ExamPaperEntity;
import com.hctforgreen.greenservice.ui.handler.RecentTerminalExamListHandler;
import com.hctforgreen.greenservice.ui.page.AbstractDataLoaderHandler;
import com.hctforgreen.greenservice.ui.page.AbstractPageableAdapter;
import com.hctforgreen.greenservice.utils.HctConstants;
/**
 * 终端培训：最新考试试题
 * @author 390694
 *2016-9-27
 */
public class RecentTerminalExamListAdapter extends
		AbstractPageableAdapter<BaseEntity> implements OnItemClickListener {

	public static final String FRESH_MSG_DATA = "com.hctforgreen.greenservice.fresh_msg_data";

	private Activity mActivity;
	private ListView mListView;
	private RecentTerminalExamListHandler mHandler;

	//private FreshDataReceiver mDataReceiver;

	public RecentTerminalExamListAdapter(ListView listView, Activity activity,
			int loadingViewResourceId,int reloadViewResourceId,
			AbstractDataLoaderHandler<BaseEntity> handler) {
		super(listView, activity, loadingViewResourceId, reloadViewResourceId,
				handler);
		mListView = listView;
		mActivity = activity;

		mHandler = (RecentTerminalExamListHandler) handler;
		mListView.setAdapter(this);
		mListView.setOnItemClickListener(this);
		mListView.setOnScrollListener(this);
		mListView.setDivider(mActivity.getResources().getDrawable(
				R.drawable.ic_driver));
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewCache viewCache;
		final ExamPaperEntity entity = (ExamPaperEntity) getItem(position);
		if (convertView == null) {
			convertView = View.inflate(mActivity,
					R.layout.item_recent_learn_data_lst, null);
			viewCache = new ViewCache(convertView);
			convertView.setTag(viewCache);
		} else {
			viewCache = (ViewCache) convertView.getTag();
		}
		viewCache.getDateTv().setText(entity.createTime);
		viewCache.getMsgTitleTv().setText(entity.name);
		viewCache.getReadedStatusCb().setVisibility(View.VISIBLE);
		
		
		viewCache.getReadedStatusCb().setChecked(true);
		

		return convertView;
	}

	/*class FreshDataReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context arg0, Intent intent) {
			if (intent != null && intent.getSerializableExtra("msg") != null) {
				MessagesEntity entity = (MessagesEntity) intent
						.getSerializableExtra("msg");
				if (entity != null) {
					for (int i = 0; i < getmList().size(); i++) {
						MessagesEntity msgEn = (MessagesEntity) getmList().get(
								i);
						if (entity.id != null && entity.id.equals(msgEn.id)) {
							msgEn.readStatus = entity.readStatus;
							getmList().set(i, msgEn);
						}
					}
					notifyDataSetChanged();
				}

			}
		}
	}*/

	public void unRegisterBroadCast() {
		//mActivity.unregisterReceiver(mDataReceiver);
	}

	class ViewCache {
		private View baseView;
		private TextView date;
		private TextView msgTitle;
		private CheckBox readedStatusCb;

		public ViewCache(View view) {
			baseView = view;
		}

		public TextView getDateTv() {
			if (date == null) {
				date = (TextView) baseView.findViewById(R.id.tv_date);
			}
			return date;
		}

		public TextView getMsgTitleTv() {
			if (msgTitle == null) {
				msgTitle = (TextView) baseView.findViewById(R.id.tv_msg_title);
			}
			return msgTitle;
		}

		public CheckBox getReadedStatusCb() {
			if (readedStatusCb == null) {
				readedStatusCb = (CheckBox) baseView
						.findViewById(R.id.cb_message_readed_status);
			}
			return readedStatusCb;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			final int position, long id) {
		
		final ExamPaperEntity entity = (ExamPaperEntity) getItem(position);
		Intent intent = new Intent();
		intent.setClass(mActivity, TerminalPaperActivity.class);
		intent.putExtra(
				HctConstants.ON_ACTIVITY_KEY_PAPER_ENTITY,
				entity);
		mActivity.startActivity(intent);

		/*new HandlerThread("update_status") {
			public void run() {
				if (entity.readStatus != 1) {
					Utils.submitPoints(mActivity, 1, "", false);

					entity.readStatus = 1;
					DBMessageManager dbMessageManager = new DBMessageManager(
							mActivity);
					dbMessageManager.updateMsg(entity);
				}
				
				
				mActivity.runOnUiThread(new Runnable() {

					@Override
					public void run() {
						getmList().set(position, entity);
						notifyDataSetChanged();

						Intent intent = new Intent();
						intent.setClass(mActivity, MessageDetailActivity.class);
						intent.putExtra(
								HctConstants.ON_ACTIVITY_KEY_MESSAGES_ENTITY,
								entity);
						mActivity.startActivity(intent);
					}
				});
			};
		}.start();*/

	}

}
